﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;


public class SlotItem : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
    private bool isRaycastLocationValid = true;

    Transform nowParent;
    /// <summary>
    /// 拖拽代码
    /// </summary>
    /// <param name="eventData"></param>
    Vector2 offset;
    public void OnBeginDrag(PointerEventData eventData)
    {

        nowParent = transform.parent;

        offset = GetComponent<RectTransform>().anchoredPosition - UIManager.Instance.getUiMousePos();

        GetComponent<RectTransform>().anchoredPosition = UIManager.Instance.getUiMousePos() + offset;

        isRaycastLocationValid = false;//设置为可以穿透
    }

    public void OnDrag(PointerEventData eventData)
    {
        GetComponent<RectTransform>().anchoredPosition = UIManager.Instance.getUiMousePos() + offset;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        GameObject go = eventData.pointerCurrentRaycast.gameObject;
        if (go != null)//落点位置不为空
        {
            Transform tragetSlot = go.transform.parent.parent;//slot
            Transform tragetSlotItem = go.transform.parent;//item
            if (tragetSlot.tag == @"Slot")
            {
                transform.SetParent(tragetSlot);
                transform.position = tragetSlot.position;

                tragetSlotItem.SetParent(nowParent);
                tragetSlotItem.position = nowParent.position;
            }
            else
            {
                print("先不管");
            }
        }
        else
        {
            print("go 空了");
        }

        isRaycastLocationValid = true;
    }

    public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
    {
        return isRaycastLocationValid;
    }
}
